﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace WcfServiceLibrary1
{
    /// <summary> 
    /// 此类是对IBookService接口的具体实现，在此类的上面我们声明了[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]标签， 
    /// 此标签代表这个类采用SingleTone（单类模式）来生成对象。 
    /// 使用[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]接口需要导入using System.ServiceModel;命名空间。 
    /// </summary> 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class PersonService : IPersonService
    {
        List<Person> _Persons = new List<Person>();
        public void AddPerson(Person person)
        {
            YuNenEntities db = new YuNenEntities();
            var tempmember = db.tbMemberInfo.Find(1);
            tempmember.Member_Name = Guid.NewGuid().ToString();
            db.tbMemberInfo.Add(tempmember);
            db.SaveChanges();
        }

        //public string GetAllPersons(int id)
        //{
        //    YuNenEntities db = new YuNenEntities();
        //    return db.tbMemberInfo.Find(id).Member_Name ;
        //}

        //异步
        public async Task<string> GetAllPersonsAsync(int id)
        {
            YuNenEntities db = new YuNenEntities();
            return await GetAllPersonsFromRemoteAsync(id);
        }

        static Task<string> GetAllPersonsFromRemoteAsync(int id)
        {
            //写法1
            //YuNenEntities db = new YuNenEntities();
            //return Task.FromResult<string>(db.tbMemberInfo.Find(id).Member_Name);
            //return db.tbMemberInfo.Find(id).Member_Name; 这个会报错

            //写法2
            return Task.Run<string>(() =>
            {
                YuNenEntities db = new YuNenEntities();
                return db.tbMemberInfo.Find(id).Member_Name;
            });
        }

        /// <summary>
        /// 1、没有导航属性的ORM实体可以正常序列化和反序列化
        /// 2、有导航属性的实体要修改db属性才能正常序列化和反序列化(但是转为原实体不行)
        /// 3、可以用匿名对象序列化和反序列化有导航属性的实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetAllPersonsByJson(int id)
        {
            
            YuNenEntities db = new YuNenEntities();
            //1、
            var log= db.tbLog.Find(id);
            string str = JsonConvert.SerializeObject(log);  //序列化
            tbLog loger = JsonConvert.DeserializeObject<tbLog>(str);    //反序列化

            //2、
            //db.Configuration.AutoDetectChangesEnabled;
            var member = db.tbMemberInfo.Where(s => s.Member_ID == id);
            //=false，有导航属性的实体可以正常序列化，但是在json数据中有导航属性的表，比如tbMemberInvestAmount等
            db.Configuration.ProxyCreationEnabled = false;
            //=false，有导航属性的实体可以正常序列化，同时，在json数据中没有有导航属性的表
            db.Configuration.LazyLoadingEnabled = false;
            //两者同时=false，在json数据中有导航属性的表
            var memstr = JsonConvert.SerializeObject(member);
            var mem = JsonConvert.DeserializeObject(memstr);
            //tbMemberInfo mem = JsonConvert.DeserializeObject<tbMemberInfo>(memstr);  会报错

            //3、
            var tempmember = db.tbMemberInfo.Select(s => new { s.Member_ID, s.Member_Alias, s.Member_Name, s.Phone });
            string tempmemberstr = JsonConvert.SerializeObject(tempmember);
            var a = JsonConvert.DeserializeObject(tempmemberstr);
            
            string mstr = JsonConvert.SerializeObject(member);
            
            return str;
        }

        public void RemovePerson(string id)
        {
            Person person = _Persons.Find(p => p.Id == id);

            _Persons.Remove(person);
        }
    }
}